package com.danny.Controller;

import com.danny.mapper.SumInfoMapper;
import com.danny.pojo.CheckIn;
import com.danny.pojo.Item;
import com.danny.pojo.SumInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SumInfoController {
    @Autowired
    private SumInfoMapper sumInfoMapper;

    @Autowired
    private ItemController itemController;

    @Autowired
    private CheckInController checkInController;

    @Autowired
    private VipController vipController;

    @GetMapping("queryAllSumInfo")
    public List<SumInfo> queryAllSumInfo(){
        return sumInfoMapper.queryAllSumInfo();
    }
    //    List<SumInfo> querySumInfoLimit(String SumInfoName,int page,int pageSize);
    @GetMapping("querySumInfoLimitByNoParam")
    public List<SumInfo> querySumInfoLimitByNoParam(int page,int pageSize){
        return sumInfoMapper.querySumInfoLimitByNoParam(page,pageSize);
    }
    @GetMapping("querySumInfoLimitByRoomNo")
    public List<SumInfo> querySumInfoLimitByRoomNo(int roomNo,int page,int pageSize){
        return sumInfoMapper.querySumInfoLimitByRoomNo(roomNo,page,pageSize);
    }

    public SumInfo calSumInfo(SumInfo sumInfo) {
        if (!(sumInfo.getItemName() == null || sumInfo.getNumber() == null)) { //计算价格
        String[] items = sumInfo.getItemName().split(",");
        String[] numbers = sumInfo.getNumber().split(",");
        int price = 0;
        String unitprice = new String();
        for (int i = 0; i < items.length; i++) {
            Item it=new Item();
            it.setItemName(items[i]);
            List<Item> list = itemController.queryItemsLimitHasInput(it, 0, 1000);
            Item item = list.get(0);
            price += item.getPrice() * Integer.parseInt(numbers[i]);
            if (i == items.length - 1) {
                unitprice += item.getPrice();
            } else unitprice += item.getPrice() + ",";

        }
        sumInfo.setSumPrice(price);
        sumInfo.setUnitPrice(unitprice);
    }
        //判断会员
        CheckIn checkIn=new CheckIn();
        checkIn.setCheckinNo(sumInfo.getCheckinNo());
        checkIn=checkInController.queryCheckInByMul(checkIn).get(0);
        if(vipController.queryVipLimit(checkIn.getCheckinName(),0,1000).isEmpty()) {
            System.out.println("empty");

            sumInfo.setDiscountRate(0);
            sumInfo.setNewPrice(sumInfo.getSumPrice());
            sumInfo.setSum(sumInfo.getSumPrice());
        }
        else {

            sumInfo.setDiscountRate(0.1);
            System.out.println((int)(sumInfo.getSumPrice()*0.9));
            sumInfo.setNewPrice((int)(sumInfo.getSumPrice()*0.9));
            sumInfo.setSum(sumInfo.getNewPrice());
        }
        return sumInfo;
    }
    @GetMapping("addSumInfo")
    public String addSumInfo(SumInfo sumInfo){
        sumInfo =calSumInfo(sumInfo);
        if(sumInfoMapper.addSumInfo(sumInfo)>0)return "inserted";
        return "error";
    }

    @GetMapping("updateSumInfo")
    public String updateSumInfo(SumInfo sumInfo){
        sumInfo =calSumInfo(sumInfo);
        if(sumInfoMapper.updateSumInfo(sumInfo)>0)return "updated";
        return "error";
    }
    @GetMapping("deleteSumInfoById")
    public String deleteSumInfoById(int checkinNo){
        if(sumInfoMapper.deleteSumInfoById(checkinNo)>0)return "deleted";
        return "error";
    }
    @GetMapping("SumInfoCount")
    public int SumInfoCount(){
        return sumInfoMapper.SumInfoCount();
    }



}
